package com.nimitz.web.controller.module.employee;

import com.nimitz.common.annotation.Log;
import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.enums.BusinessType;
import com.nimitz.common.utils.StringUtils;
import com.nimitz.module.employee.domain.EmpEmployee;
import com.nimitz.module.employee.domain.EmpInduction;
import com.nimitz.module.employee.service.IEmpEmployeeService;
import com.nimitz.module.employee.service.IEmpInductionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 入职维护
 */
@RestController
@Api(tags = "人员管理-入职记录")
@RequestMapping("/module/employee/induction")
public class EmpInductionController extends BaseController {

    @Resource
    private IEmpInductionService inductionService;
    @Resource
    private IEmpEmployeeService employeeService;

    /**
     * 修改入职
     *
     * @param induction 入职
     * @return 结果
     */
    @PutMapping
    @ApiOperation(value = "修改入职")
    @PreAuthorize("@ss.hasPermi('employee:induction:add')")
    @Log(title = "修改入职", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody EmpInduction induction) {
        EmpEmployee employee = new EmpEmployee();
        employee.setEmployeeId(induction.getEmployeeId());
        employee.setEmployeeCode(induction.getInductionEmployee().getEmployeeCode());
        employee.setEmployeeName(induction.getInductionEmployee().getEmployeeName());
        if (!employeeService.checkEmployeeCodeUnique(employee)) {
            return error("办理入职'" + employee.getEmployeeName() + "'失败，人员警号已存在!");
        }
        induction.setUpdateBy(getUsername());
        return toAjax(inductionService.updateInduction(induction));
    }

    /**
     * 根据入职id查询入职
     *
     * @param inductionId 入职id
     * @return 入职
     */
    @GetMapping("/{inductionId}")
    @ApiOperation(value = "根据入职id查询入职")
    public AjaxResult getInfo(@PathVariable(value = "inductionId", required = false) Integer inductionId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(inductionId)) {
            EmpInduction induction = inductionService.selectInductionById(inductionId);
            ajax.put(AjaxResult.DATA_TAG, induction);
        }
        return ajax;
    }

    /**
     * 根据条件查询入职列表（含分页）
     *
     * @param induction 入职
     * @return 入职集合
     */
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('employee:induction:list')")
    @ApiOperation(value = "根据条件查询入职列表（含分页）")
    public TableDataInfo list(@RequestBody EmpInduction induction) {
        startPage();
        List<EmpInduction> list = inductionService.selectInductionList(induction);
        return getDataTable(list);
    }

    /**
     * 批量提交
     *
     * @param inductionIds 需要提交的入职id集合
     * @return 结果
     */
    @PutMapping("/submit/{inductionIds}")
    @ApiOperation(value = "批量提交")
    @PreAuthorize("@ss.hasPermi('employee:induction:submit')")
    @Log(title = "入职提交", businessType = BusinessType.UPDATE)
    public AjaxResult submit(@PathVariable Integer[] inductionIds) {
        return toAjax(inductionService.submitInduction(getUsername(), inductionIds));
    }

    /**
     * 批量驳回
     *
     * @param inductionIds 需要驳回的入职id集合
     * @return 结果
     */
    @PutMapping("/reject/{inductionIds}")
    @ApiOperation(value = "批量驳回")
    @PreAuthorize("@ss.hasPermi('employee:induction:reject')")
    @Log(title = "入职驳回", businessType = BusinessType.UPDATE)
    public AjaxResult reject(@PathVariable Integer[] inductionIds) {
        return toAjax(inductionService.rejectInduction(getUsername(), inductionIds));
    }

    /**
     * 批量审核
     *
     * @param inductionIds 需要驳回的入职id集合
     * @return 结果
     */
    @PutMapping("/audit/{inductionIds}/{employeeIds}")
    @ApiOperation(value = "批量审核")
    @PreAuthorize("@ss.hasPermi('employee:induction:audit')")
    @Log(title = "入职审核", businessType = BusinessType.UPDATE)
    public AjaxResult audit(@PathVariable Integer[] inductionIds, @PathVariable Integer[] employeeIds) {
        return toAjax(inductionService.auditInduction(getUsername(), inductionIds, employeeIds));
    }
}
